<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>Layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" th:href="@{/layui/css/layui.css}" media="all">
    <script th:src="@{/layui/layui.js}" charset="utf-8"></script>
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>
<br>
<form class="layui-form" action="" id="searchForm">
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">WeChat号</label>
            <div class="layui-input-inline">
                <input type="text" name="wechat_no" id="wechat_no" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">WeChat备注</label>
            <div class="layui-input-inline">
                <input type="text" name="wechat_mark" id="wechat_mark" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">QQ</label>
            <div class="layui-input-inline">
                <input type="text" name="qq_no" id="qq_no" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
                <button type="button" onclick="doSearch();" class="layui-btn" lay-submit="" lay-filter="demo1">搜索</button>
                <button type="reset" class="layui-btn ">reset</button>

            </div>
        </div>
    </div>

</form>
<table class="layui-hide" id="test" lay-filter="test"></table>

<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中行数据</button>
        <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>
        <button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选</button>
        <button class="layui-btn layui-btn-sm" lay-event="add">添加</button>
    </div>
</script>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>


<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->

<script>
    var $, table;
    layui.use('table', function(){
         table = layui.table;
        $=layui.jquery;
        //温馨提示：默认由前端自动合计当前行数据。从 layui 2.5.6 开始： 若接口直接返回了合计行数据，则优先读取接口合计行数据。
        //详见：https://www.layui.com/doc/modules/table.html#totalRow
        table.render({
            elem: '#test'
            ,url:'/courseorder/listJson'
            ,toolbar: '#toolbarDemo'
            ,title: '用户数据表'
            ,totalRow: true
            ,cols: [
                [
                {type: 'checkbox', fixed: 'left'}
               // ,{field:'id', title:'ID', width:80, fixed: 'left', unresize: true, sort: true, totalRowText: '合计'}
                ,{field:'name', title:'名字', width:120}
                ,{field:'wechat_nowechat_mark', title:'微信备注', width:120}
                ,{field:'wechat_no', title:'微信ID', width:120}
                ,{field:'qq_no', title:'QQ号', width:120}
                ,{field:'course_name', title:'购买课程', width:120}
                ,{field:'course_price', title:'价格', width:120}
                ,{field:'order_date', title:'订单日期', width:120}
                ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:150}
            ]
            ]
            ,page: true
            ,id:'orderTable'
        });

        //工具栏事件
        table.on('toolbar(test)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'getCheckData':
                    var data = checkStatus.data;
                    layer.alert(JSON.stringify(data));
                    break;
                case 'getCheckLength':
                    var data = checkStatus.data;
                    layer.msg('选中了：'+ data.length + ' 个');
                    break;
                case 'isAll':
                    layer.msg(checkStatus.isAll ? '全选': '未全选')
                    break;
                case 'add':
                    layer.msg('弹出添加界面');
                    layer.open({
                        type:2,
                        offset:'auto',
                        title:'添加课程订单',
                        content:'/courseorder/add',
                        area:['1000px','600px'],
                        btn:'关闭全部',
                        btnAlign:'c',
                        yes:function () {
                           layer.closeAll();
                        }
                    })
                    break;
            };
        });

        //双击事件
        table.on('rowDouble(test)',function (obj) {
            var order_id=obj.data.order_id;
            //alert(order_id);
            layer.open({
                type:2,
                offset:'auto',
                title:'课程订单详情',
                content:'/courseorder/detail?order_id='+order_id,
                area:['1000px','600px'],
                btn:'关闭全部',
                btnAlign:'c',
                yes:function () {
                    layer.closeAll();
                }
            })

        })
        //监听工具条
        table.on('tool(test)', function(obj){ //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var orderId = obj.data.order_id; //获得order_id
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）


            if(layEvent === 'del'){ //删除
                layer.confirm('真的删除行么', function(index){
                    obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                    layer.close(index);
                    //向服务端发送删除指令
                    $.get("/courseorder/delete",{order_id:orderId},function (respData) {
                        console.log(respData);
                        if (respData.success==1){
                            layer.msg("删除成功")
                        }else {
                            layer.msg("删除失败"+resp.message);
                        }
                    })


                });
            } else if(layEvent === 'edit'){ //编辑
                layer.open({
                    type:2,
                    offset:'auto',
                    title:'修改课程订单',
                    content:'/courseorder/edit?order_id='+orderId,
                    area:['1000px','600px'],
                    btn:'关闭全部',
                    btnAlign:'c',
                    yes:function () {
                        layer.closeAll();
                    }
                });
            }

        });

    });
    function doSearch(){
        //通过jquery获取参数
        var wechat_no=$("#wechat_no").val();
        var wechat_mark=$("#wechat_mark").val();
        var qq_no=$("#qq_no").val();
        table.reload('orderTable',{
            where:{
                wechat_no:wechat_no,
                wechat_mark:wechat_mark,
                qq_no:qq_no,
            },page:{
                curr: 1
            }
        });
    }

</script>

</body>
</html>